const tab = {
  state: {
    tabMap: new Map(),
    tabList: new Array()
  },
  mutations: {
    SET_TAB_MAP: (state, currentRouter) => {
    //   console.log("进入SET_TAB_MAP......")
      let newRoute = {...currentRouter};
      newRoute.meta = {...currentRouter.meta};
    //   console.log("newRoute",newRoute)
      if(currentRouter.meta.menuType == 3 || currentRouter.meta.menuType == 4 ){
        let matched = currentRouter.matched;
        let parent = matched[matched.length - 1].parent;
        // console.log("parent",parent)
        newRoute.meta.name = parent.meta.name;
        newRoute.meta.key = parent.meta.key;
        state.tabMap.set(newRoute.meta.key,newRoute); 
      }else{
        if(newRoute.path.indexOf('/crm/customer/detail') > -1 ){
          newRoute.meta.name = newRoute.params.customerName;
          newRoute.meta.key +=  newRoute.params.customerId;
        }
        state.tabMap.set(newRoute.meta.key,newRoute); 
      }
    //   console.log("tab_router:",newRoute);
      state.tabList = [...state.tabMap.values()];
    },
    DELETE_TAB:(state, key) =>{
      state.tabMap.delete(key);
      state.tabList = [...state.tabMap.values()];
    }
  },
  actions: {
    push({ commit }, currentRouter) {
      return new Promise(resolve => {
        commit("SET_TAB_MAP",currentRouter)
        resolve();
      });
    },
    delete({ commit },key){
      return new Promise(resolve => {
        commit("DELETE_TAB",key)
        resolve();
      });
    }
  }
};

export default tab;
